package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edh.domain.EdhShoppingCart;
import com.ruoyi.edh.domain.model.EdhShoppingCartModel;
import com.ruoyi.edh.domain.vo.EdhShoppingCartVo;
import com.ruoyi.edh.service.IEdhShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 易订货-购物车Controller
 *
 * @author xxx
 * @date 2020-09-02
 */
@RestController
@RequestMapping("/edh/cart")
@Api(description = "易订货-购物车")
public class EdhShoppingCartController extends BaseController {
    @Autowired
    private IEdhShoppingCartService edhShoppingCartService;

    /**
     * 查询易订货-购物车列表
     */
    @GetMapping("/list")
    @ApiOperation("易订货-购物车列表")
    public /*TableDataInfo*/ AjaxResult list(EdhShoppingCart edhShoppingCart) {
        // 暂不分页 分页需重写分页方法
        //startPage();
        edhShoppingCart.setCreateBy(SecurityUtils.getUserId().toString());
        List<EdhShoppingCartVo> list = edhShoppingCartService.selectEdhShoppingCartList(edhShoppingCart);
        Map<String, Object> map = edhShoppingCartService.sortShoppingCart(list);
        return AjaxResult.success(map);
        // return getDataTable(list);
    }

    /**
     * 导出易订货-购物车列表
     */
    @Log(title = "易订货-购物车", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出易订货-购物车列表")
    public AjaxResult export(EdhShoppingCart edhShoppingCart) {
        List<EdhShoppingCartVo> list = edhShoppingCartService.selectEdhShoppingCartList(edhShoppingCart);
        ExcelUtil<EdhShoppingCartVo> util = new ExcelUtil<EdhShoppingCartVo>(EdhShoppingCartVo.class);
        return util.exportExcel(list, "购物车清单");
    }

    /**
     * 获取易订货-购物车详细信息
     */
    @GetMapping
    @ApiOperation("获取易订货-购物车详细信息")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(edhShoppingCartService.selectEdhShoppingCartById(id));
    }

    /**
     * 新增易订货-购物车
     */
    @Log(title = "易订货-购物车", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增购物车商品")
    public AjaxResult add(@Validated @RequestBody EdhShoppingCartModel.Insert edhShoppingCart) {
        EdhShoppingCart cart = new EdhShoppingCart();
        cart.setCreateBy(SecurityUtils.getUserId().toString());
        cart.setMicroshopId(edhShoppingCart.getMicroshopId());
        cart.setAmount(edhShoppingCart.getAmount());
        cart.setGoodsSpecificationsId(edhShoppingCart.getGoodsSpecificationsId());
        return toAjax(edhShoppingCartService.insertEdhShoppingCart(cart));
    }

    /**
     * 设置为默认收货地址
     */
    @Log(title = "设置为默认收货地址", businessType = BusinessType.INSERT)
    @PostMapping("/defaultAdress")
    @ApiOperation("设置为默认收货地址")
    public AjaxResult defaultAdress(Long id) {
        return toAjax(edhShoppingCartService.defaultAdress(id));
    }

    /**
     * 修改易订货-购物车
     */
    @Log(title = "易订货-购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改购物车数量")
    public AjaxResult edit(@Validated @RequestBody EdhShoppingCartModel.Update edhShoppingCart) {
        EdhShoppingCart cart = new EdhShoppingCart();
        cart.setUpdateBy(SecurityUtils.getUserId().toString());
        cart.setId(edhShoppingCart.getShoppingCartId());
        cart.setAmount(edhShoppingCart.getAmount());
        return toAjax(edhShoppingCartService.updateEdhShoppingCart(cart));
    }

    /**
     * 删除易订货-购物车
     */
    @Log(title = "易订货-购物车", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    @ApiOperation("删除易订货-购物车")
    public AjaxResult remove(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return AjaxResult.error("请选择要删除的购物车");
        }
        return toAjax(edhShoppingCartService.deleteEdhShoppingCartByIds(ids.split(",")));
    }
}
